package com.link.base.base.reception.controller;

import com.link.base.base.reception.model.*;
import com.link.base.base.reception.service.ReceptionService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.base.base.reception.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接待
 *
 * @author unknown
 * @date unknown
 */
@Controller
@RequestMapping("/link/reception/app/guide")
public class ReceptionController extends BasicController<Reception> {

    @Resource
    private ReceptionService receptionService;

    @Override
    public BasicService<Reception> getBasicService() throws Exception {
        return receptionService;
    }

    /**
     * 提交下一步
     *
     * @author lizt 2017年3月8日
     */
    @RequestMapping(value = "/optyCommit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> optyCommit(@RequestBody Reception record, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();

        try {
            receptionService.optyCommit(record);
            Reception qo = receptionService.queryMoreByOptyId(record);
            result.put("result", qo);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 通过ID查询商机信息、测量信息及意向产品
     *
     * @author lizt 2017年3月9日
     */
    @RequestMapping(value = "/queryMoreByOptyId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryMoreById(Reception record, HttpSession session, HttpServletRequest request,
                                             HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> followUpMap = new HashMap<>();
        try {
            /*查询跟进*/
            followUpMap = receptionService.queryFollowUpByOptyId(record);
            result.put("textFollowUp", followUpMap.get("textFollowUp"));
            result.put("measureInfoList", followUpMap.get("measureInfoList"));
            result.put("workplanList", followUpMap.get("workplanList"));
            Reception qo = receptionService.queryMoreByOptyId(record);
            result.put("result", qo);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 通过商机id查询测量信息
     *
     * @author ouyangzhenqiang 2017年3月16日
     */
    @RequestMapping(value = "/getOptyMeasure", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getOptyMeasure(HttpSession session, HttpServletRequest request,
                                              HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        /* 商机id */
        String parentId = request.getParameter("parentId");

        try {
            List<MeasureInfo> list = receptionService.getOptyMeasure(Long.parseLong(parentId));
            result.put("result", list);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 提交测量信息
     *
     * @author ouyangzhenqiang 2017年3月17日
     */
    @RequestMapping(value = "/commitOptyMeasure", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> commitOptyMeasure(@RequestBody ListOfMeasure list, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();

        try {
            receptionService.commitOptyMeasure(list);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/upsert")
    @ResponseBody
    @Override
    public Map<String, Object> upsert(@RequestBody Reception entity, HttpSession session, HttpServletRequest request,
                                      HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> followUpMap = new HashMap<>();
        try {
            beforUpsert(entity, request);
            checkData(entity, request);

            receptionService.upsert(entity);
            Reception newEntry = receptionService.queryMoreByOptyId(entity);
            /*查询跟进*/
            followUpMap = receptionService.queryFollowUpByOptyId(newEntry);
            result.put("textFollowUp", followUpMap.get("textFollowUp"));
            result.put("measureInfoList", followUpMap.get("measureInfoList"));
            result.put("workplanList", followUpMap.get("workplanList"));
            result.put("result", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @Override
    public void beforQueryAllOrExample(Reception entity, HttpServletRequest request) throws Exception {

//      super.beforQueryAllOrExample(entity, request);
//      entity.setBuyReason(request.getParameter("buyReason"));
//      entity.setCustomerPersuasiveness(request.getParameter("customerPersuasiveness"));
//      entity.setGuideId(request.getParameter("guideId"));
//      entity.setFilterParm1(request.getParameter("filterParm1"));
//      entity.setFilterParm2(request.getParameter("filterParm2"));
//      entity.setSearchText(request.getParameter("searchText"));
    }

    /**
     * 跟进时间进度生成新的任务（用于手机端手动生成）
     *
     * @author lizt 2017年8月18日
     */
    @RequestMapping(value = "/generateTask", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> generateTask(@RequestBody GuideSchedule record, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            record = receptionService.generateTask(record);
            result.put("result", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    /**
     * @author : LiQun
     * @date : 下午2:37 2018/4/12
     * @dscription : 根据商机ID查询意向产品
     */

    @RequestMapping(value = "/queryIntendProdByOptyId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryIntendProdByOptyId(OptyIntendProduct record, HttpSession session,
                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<OptyIntendProduct> intendProductList = receptionService.queryIntendProdByOptyId(record);
            result.put("result", intendProductList);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 查询待接待列表
     *
     * @author LiQun
     * @date 上午11:55 2018/5/4
     */

    @RequestMapping(value = "/queryWaitForReceptions", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryWaitForReceptions(@JsonParam WaitReception reception, HttpSession session,
                                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<>();

        try {
            Long storeId = reception.getStoreId();
            if (null == storeId) {
                throw new ServiceException("ECEPRTION-001");
            }
            List<WaitReception> receptionList = receptionService.queryWaitForReceptionsPage(reception);
            result.put("success", true);
            result.put("rows", receptionList);
            result.put("total", getCount(reception, receptionList.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("message", e.getMessage());
        }
        return result;
    }


    /**
     * 查询待接待客户列表总数
     *
     * @author ouyangzhenqiang
     * @date 2018/11/30
     */
    @RequestMapping(value = "/queryWaitReceptionListNum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryWaitReceptionListNum(@RequestBody WaitReception reception, HttpSession session,
                                                         HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<>();

        try {
            Long storeId = reception.getStoreId();
            if (null == storeId) {
                throw new ServiceException("RECEPTION-001");
            }
            int num = receptionService.queryWaitReceptionListNum(reception);
            result.put("success", true);
            result.put("result", num);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据接待头查询接待列表
     *
     * @author LiQun
     * @date 下午1:57 2018/5/4
     */
    @RequestMapping(value = "/queryReceptionListByHeadId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryReceptionListByHeadId(WaitReceptionList reception) throws Exception {
        Map<String, Object> result = new HashMap<>();

        try {
            List<WaitReceptionList> receptionList = receptionService.queryReceptionListByHeadId(reception);
            result.put("success", true);
            result.put("result", receptionList);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据接待头查询接待头本身及其子接待列表，返回以接待为头的头行完整对象，用语新形式的接待提示框
     *
     * @author ouyangzhenqiang
     * @date 2018/12/1
     */
    @RequestMapping(value = "/queryReceptionByHeadId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryReceptionByHeadId(WaitReception reception) throws Exception {
        Map<String, Object> result = new HashMap<>();

        try {
            WaitReception record = receptionService.queryReceptionByHeadId(reception);
            result.put("success", true);
            result.put("result", record);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 营销助手未登录状态下分享
     * @author 旷锐锋
     * @date 2019/2/21
     */
    @RequestMapping(value = "/notLogin/queryByExamplePage")
    @ResponseBody
    public Map<String, Object> query(@JsonParam Reception reception, @JsonParam QueryParams qps,
                                     HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            result = super.queryByExamplePage(qps, reception, session, request, response);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

}
